﻿using Microsoft.VisualBasic;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace ORMapperOef
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void Form1_Load(object sender, EventArgs e)
        {
            LoadArtists();
        }

#region "LoadLists"
        private void LoadArtists()
        {
            using (var db = new DomainContext())
            {
                var query = from a in db.Artists
                            where (!String.IsNullOrEmpty(a.Name))
                            orderby a.Name
                            select a;

                lbArtists.DataSource = query.ToList();
            }
        }

        private void LoadAlbums(Artist artist)
        {
            using (var db = new DomainContext())
            {
                var query = from a in db.Albums
                            where ((!String.IsNullOrEmpty(a.Name)) && a.ArtistId == artist.ArtistId)
                            orderby a.Name
                            select a;

                lbAlbums.DataSource = query.ToList();
            }
        }

        private void LoadTracks(Album album)
        {
            using (var db = new DomainContext())
            {
                var query = from a in db.Tracks
                            where ((!String.IsNullOrEmpty(a.Name)) && a.AlbumId == album.AlbumId)
                            orderby a.Name
                            select a;

                lbTracks.DataSource = query.ToList();
            }
        }

        private void LoadGenres(Album album)
        {
            using (var db = new DomainContext())
            {
                var query = from a in db.Genres
                            where ((!String.IsNullOrEmpty(a.Name)) && a.AlbumId == album.AlbumId)
                            orderby a.Name
                            select a;

                lbGenres.DataSource = query.ToList();
            }
        }
#endregion

        private void lbAlbums_SelectedIndexChanged(object sender, EventArgs e)
        {
            var album = lbAlbums.SelectedItem as Album;
            if (album != null)
            {
                LoadTracks(album);
                LoadGenres(album);
            }
        }
        
        private void lbArtists_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (lbArtists.SelectedItem != null) LoadAlbums(lbArtists.SelectedItem as Artist);
        }

#region "Add"
        private void cmdAddArtist_Click(object sender, EventArgs e)
        {
            var input = Interaction.InputBox("Enter the name of the new artist.");
            var artist = new Artist { Name = input };

            using (var db = new DomainContext())
            {
                db.Artists.Add(artist);
                db.SaveChanges();
            }

            LoadArtists();
        }
        
        private void cmdAddAlbum_Click(object sender, EventArgs e)
        {
            if (lbArtists.SelectedItem == null) return;

            var input = Interaction.InputBox("Enter the name of the new album.");
            
            var artist = lbArtists.SelectedItem as Artist;
            if (artist != null)
            {
                var album = new Album
                    {
                        Name = input,
                        ArtistId = artist.ArtistId
                    
                    };

                using (var db = new DomainContext())
                {
                    db.Albums.Add(album);
                    db.SaveChanges();
                }

                LoadAlbums(artist);
            }
        }
        
        private void cmdAddTrack_Click(object sender, EventArgs e)
        {
            if (lbAlbums.SelectedItem == null) return;

            var input = Interaction.InputBox("Enter the name of the new album.");

            var album = lbAlbums.SelectedItem as Album;
            if (album != null)
            {
                var track = new Track
                {
                    Name = input,
                    AlbumId = album.AlbumId

                };

                using (var db = new DomainContext())
                {
                    db.Tracks.Add(track);
                    db.SaveChanges();
                }

                LoadTracks(album);
            }
        }

        private void cmdAddGenre_Click(object sender, EventArgs e)
        {
            if (lbAlbums.SelectedItem == null) return;

            var input = Interaction.InputBox("Enter the name of the new album.");

            var album = lbAlbums.SelectedItem as Album;
            if (album != null)
            {
                var genre = new Genre
                {
                    Name = input,
                    AlbumId = album.AlbumId

                };

                using (var db = new DomainContext())
                {
                    db.Genres.Add(genre);
                    db.SaveChanges();
                }

                LoadGenres(album);
            }
        }
#endregion
    }
}
